/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.layers;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.LayerInfo;
import nl.cloudfarming.client.geoviewer.MapController;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

/**
 * Action for toggling the visibility of a layer.
 * 
 * @author Timon Veenstra
 */
public class VisibilityLayerAction extends AbstractAction {

    private static final String KEY_ACTION_HIDE = "action.name.hide";
    private static final String KEY_ACTION_SHOW = "action.name.show";
    private static final long serialVersionUID = -1222090203615272257L;

    public VisibilityLayerAction(LayerInfo layerInfo) {
        if (layerInfo.isVisible()) {
            putValue(NAME, NbBundle.getMessage(this.getClass(), KEY_ACTION_HIDE));
        } else {
            putValue(NAME, NbBundle.getMessage(this.getClass(), KEY_ACTION_SHOW));
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Layer layer = Utilities.actionsGlobalContext().lookup(Layer.class);
        MapController mapApi = Utilities.actionsGlobalContext().lookup(MapController.class);
        assert mapApi != null;
        assert layer != null;
        // toggle visibility
        mapApi.getLayerInfo(layer).setVisible(!mapApi.getLayerInfo(layer).isVisible());
        LayerListTopComponent.findInstance().refresh();
    }
}
